package com.common.widget;

import com.common.utils.StarAppUtils;
import com.starappallcommon.R;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 通用信息提示框
 * 
 * <br>Created 2014-11-17 下午5:25:26
 * @version  
 * @author   jiangwb		
 *
 * @see
 */
public class CommonCustomDlg extends Dialog {
	/** 上下文 */
	private Context mContext;

	/** 提示信息文字textview */
	private TextView mtvTip;

	/** 提示信息文字 */
	private String mstrTip;

	/** 自定义的内容View  */
	private View mCustomView;

	/** 左边按钮 */
	private Button mbtnLeft;
	/** 左边按钮文字 */
	private String mstrLeft;

	/** 右边按钮 */
	private Button mbtnRight;
	/** 右边按钮文字 */
	private String mstrRight;

	/** 按钮点击监听 */
	private OnCommonDlgClickListener mListener;

	/**所加载的layout资源ID*/
	private int mLayoutResID = 0;

	/**
	 * ClassName:OnCommonDlgClickListener Function: 按钮点击监听接口 Reason: TODO ADD
	 * REASON
	 * 
	 * @author Administrator
	 * @version CommonTipDlg
	 * @since Ver 1.1
	 * @Date 2014 2014-4-24 上午10:37:14
	 * 
	 * @see
	 */
	public interface OnCommonDlgClickListener {
		/**
		 * 左按钮点击处理
		 * 
		 * <br>Created 2014-11-17 下午5:44:48
		 * @author        :  jiangwb
		 */
		void onLeftBtnClicked();

		/**
		 * 右按钮点击处理
		 * 
		 * <br>Created 2014-6-9 下午5:45:16
		 * @author        :  linsj
		 */
		void onRightBtnClicked();
	}

	/**
	 * 设置监听
	 * 
	 * <br>Created 2014-11-17下午5:29:56
	 * @param listener 监听器
	 * @author        :  jiangwb
	 */
	public void setOnListener(OnCommonDlgClickListener listener) {
		this.mListener = listener;
	}

	/**
	 * Creates a new instance of CommonTipDlg.
	 * 
	 * @param context
	 *            　上下文
	 * @param strTip
	 *            　　提示信息
	 * @param strLeftBtn
	 *            　左边按钮文字
	 * @param strRightBtn
	 *            　右边按钮文字
	 * @param Resid
	 *            　　　对话框样式ID
	 */
	public CommonCustomDlg(Context context, String strTip, String strLeftBtn,
			String strRightBtn, int Resid) {
		super(context, Resid);
		mContext = context;
		mstrTip = strTip;
		mstrLeft = strLeftBtn;
		mstrRight = strRightBtn;
		mLayoutResID = R.layout.common_costom_dlg;
	}

	/**
	 * 设置要加载的布局资源ID
	 * 
	 * <br>Created 2014-11-17 下午2:12:33
	 * @param layoutResID
	 * @author        :  jiangwb
	 */
	public void setLayoutResID(int layoutResID){
		mLayoutResID = layoutResID;
	}

	/**
	 * 设置要加载的布局资源ID
	 * 
	 * <br>Created 2014-11-17 下午2:12:33
	 * @param customView
	 * @author        :  jiangwb
	 */
	public void setCustomView(View  customView){
		if(customView == null){
			return;
		}
		mCustomView = customView;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(mLayoutResID);

		//宽度设置为屏幕的7 / 8
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		final int SEVEN = 7;
		final int EIGHT = 8;
		//竖屏时，取屏幕宽度
		if(mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
			lp.width = (int) ( StarAppUtils.getInstance().readDisplayMetrics().x  * SEVEN / EIGHT);
		}else{
			//横屏时，取屏幕高度
			lp.width = (int) ( StarAppUtils.getInstance().readDisplayMetrics().y * SEVEN / EIGHT);
		}
		getWindow().setAttributes(lp);		

		mtvTip = (TextView) findViewById(R.id.tv_tip);
		mtvTip.setText(mstrTip);

		if(mCustomView != null){
			( (LinearLayout) findViewById(R.id.costom_view)).addView(mCustomView);			
		}

		mbtnLeft = (Button) findViewById(R.id.btn_left);
		mbtnLeft.setText(mstrLeft);

		mbtnRight = (Button) findViewById(R.id.btn_Right);
		mbtnRight.setText(mstrRight);

		mbtnLeft.setOnClickListener(clickListener);
		mbtnRight.setOnClickListener(clickListener);

		setTextTypeface();
	}

	/**
	 * 设置控件为兰亭序字体
	 * 
	 * <br>Created 2014-11-17 下午7:30:25
	 * @author       jiangwb
	 */
	private void setTextTypeface() {
		StarAppUtils.setViewTypeFace(mtvTip, false);
		StarAppUtils.setViewTypeFace(mbtnLeft, false);
		StarAppUtils.setViewTypeFace(mbtnRight, false);
	}

	/**
	 * 点击事件
	 */
	protected Button.OnClickListener clickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			int id = v.getId();
			if (R.id.btn_left == id) {
				if (null != mListener) {
					mListener.onLeftBtnClicked();
				}
				dismiss();
			} else if (R.id.btn_Right == id) {
				if (null != mListener) {
					mListener.onRightBtnClicked();
				}
				dismiss();
			} else {
				dismiss();
			}
		}
	};
}
